package im.rtx.core.io; /*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /**
 * Project  : CookQQ
 * Package  : net.solosky.cookqq.client.myqq.net
 * File     : io.ByteReader.java
 * Author   : solosky < solosky772@qq.com >
 * Created  : 2009-10-12
 * License  : Apache License 2.0 
 */

 import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

/**
 * <pre>
 * 字节读取类
 * 
 * 主要是以java可以识别的类型读取字节数组里数据
 * 委托DataInputStream来实现，但这个类会抛出IOException
 * 因为我们这里读取的是字符数组，永远不会抛出这个异常
 * 为了编程方便，这里catch了IOException
 *
 *  <code>
 * ChangeLog:
 *   2009-10-12	io.ByteReader.java created.
 *  </code>
 * </pre>
 */
public class ByteReader
{
	private byte[] byteArray;
	private DataInputStream in;
	
	public ByteReader(byte[] byteArray)
	{
		this.byteArray = byteArray;
		this.in = new DataInputStream(new ByteArrayInputStream(byteArray));
	}

	public final int read(byte[] b, int off, int len)
    {
		int i = 0;
		try {
			i = in.read(b, off, len);
		}catch(IOException e) {
			//never happen....
		}
		return i;
    }

	public final int read(byte[] b)
    {
	    int ret = 0;
		try {
			ret = in.read(b);
		}catch(IOException e) {
			//never happen....
		}
		return ret;
    }

	public final byte readByte()
    {
		byte b = 0;
	    
		try {
	        b=in.readByte();
        } catch (IOException e) {}
        
	    return b;
    }

	public final char readChar()
    {
		char c = 0;
	    try {
	        c = in.readChar();
        } catch (IOException e) {}
	    
	    return c;
    }

	public final int readInt()
    {
		int i = 0;
	    try {
	        i = in.readInt();
        } catch (IOException e) {}
	    
	    return i;
    }

	public final int skipBytes(int n)
    {
		int i = 0; 
	    try {
	        i = in.skipBytes(n);
        } catch (IOException e) {}
	    
	    return i;
    }
	
	public final short readShort()
    {
		short s = 0; 
	    try {
	        s = in.readShort();
        } catch (IOException e) {}
	    
	    return s;
    }


    public final String readUnicode(){
        int s = readByte();
        char [] cs = new char[s/2 - 1];
        for(int i=0; i<cs.length; i++){
          int b1 = readByte() & 0xff;
          int b2 = readByte() & 0xff;
          cs[i] = (char)((b2 << 8) + b1);
        }
        readShort(); //\0
        return new String(cs);
    }

    public final String readUnicode2(){
        int s = readShort();
        char [] cs = new char[s/2 - 1];
        for(int i=0; i<cs.length; i++){
            int b1 = readByte() & 0xff;
            int b2 = readByte() & 0xff;
            cs[i] = (char)((b2 << 8) + b1);
        }
        readShort(); //\0
        return new String(cs);
    }
	
	

	
	
	
}
